//
//  ViewController.m
//  MasonryTest
//
//  Created by Charles on 16/9/23.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLMasonryViewController.h"
#import "CLContainerViewController.h"
#import "CLBasicView.h"
#import "CLConstantsView.h"
#import "CLSidesView.h"
#import "CLAspectFitView.h"
#import "CLAnimatedView.h"
#import "CLDebuggingView.h"
#import "CLLabelView.h"
#import "CLUpdateView.h"
#import "CLRemakeView.h"
#import "CLScrollView.h"
#import "CLArrayView.h"
#import "CLAttributeChangingView.h"
#import "CLMarginView.h"
#import "CLContainerTableViewCell.h"
#import "CLTestPageView.h"

static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";

@interface CLMasonryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) UITableView * tableView;
@property (nonatomic, strong) NSArray *exampleControllers;

@end

@implementation CLMasonryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
}

- (void)setupSubviews{
    self.title = @"自动布局";
    [self.view addSubview:self.tableView];
//    [self.tableView registerClass:CLContainerTableViewCell.class forCellReuseIdentifier:kMASCellReuseIdentifier];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    self.exampleControllers = @[
                                [[CLContainerViewController alloc] initWithTitle:@"Basic"
                                                                       viewClass:CLBasicView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Update Constraints"
                                                                       viewClass:CLUpdateView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Remake Constraints"
                                                                       viewClass:CLRemakeView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Using Constants"
                                                                       viewClass:CLConstantsView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Composite Edges"
                                                                       viewClass:CLSidesView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Aspect Fit"
                                                                       viewClass:CLAspectFitView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Basic Animated"
                                                                       viewClass:CLAnimatedView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Debugging Helpers"
                                                                       viewClass:CLDebuggingView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Bacony Labels"
                                                                       viewClass:CLLabelView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"UIScrollView"
                                                                       viewClass:CLScrollView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Array"
                                                                       viewClass:CLArrayView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Attribute Chaining"
                                                                       viewClass:CLAttributeChangingView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"Margins"
                                                                       viewClass:CLMarginView.class],
                                [[CLContainerViewController alloc] initWithTitle:@"CLTestPageView"
                                                                       viewClass:CLTestPageView.class]
                                ];
    
//    if ([UIViewController instancesRespondToSelector:@selector(topLayoutGuide)])
//    {
//        self.exampleControllers = [self.exampleControllers arrayByAddingObject:[[MASExampleLayoutGuideViewController alloc] init]];
//    }
    
    [self.tableView reloadData];
}

#pragma mark - 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.exampleControllers.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIViewController *viewController = self.exampleControllers[indexPath.row];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CLContainerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMASCellReuseIdentifier];
    if (cell == nil) {
        cell = [[CLContainerTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMASCellReuseIdentifier];
    }
    if (indexPath.row < self.exampleControllers.count) {
        UIViewController *viewController = self.exampleControllers[indexPath.row];
        cell.titleLabel.text = viewController.title;
        cell.iconView.image = [UIImage imageNamed:@"meinv2.jpg"];
    }
    return cell;
}

#pragma mark - 子视图

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
